package com.software.levelupserver.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.software.levelupserver.entity.SecurityUserDetails;
import com.software.levelupserver.entity.User;
import com.software.levelupserver.service.UserService;
import com.software.levelupserver.utils.JwtUtil;
import com.software.levelupserver.utils.ResultVOUtil;
import com.software.levelupserver.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class AppAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private JwtUtil jwtUtils;
    @Resource
    private UserService userService;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException {
        userService.updateUserLoginTime();
        SecurityUserDetails securityUserDetails = (SecurityUserDetails) authentication.getPrincipal();
        User user = securityUserDetails.getUser();
        List<SimpleGrantedAuthority> authorities = (List<SimpleGrantedAuthority>) securityUserDetails.getAuthorities();
        String strUserInfo = objectMapper.writeValueAsString(user);
        List<String> authList = authorities.stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.toList());
        String serverToken = jwtUtils.createJwt(strUserInfo, authList);
        User userAllDetails = userService.getUserByEmail(user.getEmail());
        User userDetails = new User(userAllDetails.getUserId(),userAllDetails.getUsername(),userAllDetails.getEmail(),userAllDetails.getGradeId(),userAllDetails.getExperience(),userAllDetails.getAvatar(),userAllDetails.getSignature(),userAllDetails.getSex(),userAllDetails.getLoginTime());
        Map token = new HashMap();
        token.put("serverToken",serverToken);
        token.put("userDetails",userDetails);
        Result success = ResultVOUtil.success(token);
        printToken(httpServletRequest,httpServletResponse,success);
    }
    private void printToken(HttpServletRequest request, HttpServletResponse response, Result httpResult) throws IOException {
        String strResponse = objectMapper.writeValueAsString(httpResult);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.println(strResponse);
        writer.flush();
    }
}
